<?php
namespace app\admin\my;

class MyPage extends My {
    private $_table = array();
    private $_keyList = array();
    private $_buttonList = array();
    private $_pagination = array();
    private $_data = array();
    private $_db = '';


    private $_statusUrl = "setStatus"; // 状态修改连接
    private $_deleteTrueUrl = "delete"; // 彻底删除连接

    private $_search = array();
    private $_select = array();
    private $_form = array();
    
     // -- 状态修改链接
    public function setDb($db) {
        $this->_db = $db;
        return $this;
    }
    public function title($title) {
        $this->_table['title'] = $title;
        return $this;
    }
    public function suggest($suggest) {
        $this->_table['suggest'] = $suggest;
        return $this;
    }
    public function tips($content) { // --未使用
        $this->_table['tips'] = $content;
        return $this;
    }
    public function url($url='') {
    	$this->_table['url'] = $url;
    	return $this;
    }
    public function height($length=1000) {
    	$this->_table['height'] = $length;
    	return $this;
    }
    public function showHeader($flag=false) {
    	$this->_table['show_header'] = $flag;
    	return $this;
    }
    public function showFooter($flag=true) {
    	$this->_table['show_footer'] = $flag;
    	return $this;
    }
    // --- 按钮
    public function buttonsAlign($align='left') { // 按钮布局
    	$this->_table['buttons_align'] = $align;
    	return $this;
    }
    public function showRefresh($flag=false) { // 显示 刷新 按钮
    	$this->_table['show_refresh'] = $flag;
    	return $this;
    }
    public function showMap($flag=true) { // 显示 过滤 按钮
    	$this->_table['show_filter'] = $flag;
    	return $this;
    }
    public function showToggle($flag=false) { // 显示 数据方式切换 按钮
    	$this->_table['show_toggle'] = $flag;
    	return $this;
    }
    public function showColumns($flag=false) { // 显示 字段过滤 按钮
    	$this->_table['show_columns'] = $flag;
    	return $this;
    }
    public function showPaginationSwitch($flag=false) { // 显示 分页 按钮
    	$this->_table['show_pagination_switch'] = $flag;
    	return $this;
    }
    public function pagination($flag=false) {
		$this->_table['pagination'] = $flag;
    	return $this;
    }
    public function paginationLoop($flag=false) { //--有问题
    	$this->_table['pagination_loop'] = $flag;
    	return $this;
    }
    public function pageNumber($number=1) {
    	$this->_table['page_number'] = $number;
    	return $this;
    }
    public function pageSize($size=20) {
    	$this->_table['page_size'] = $size;
    	return $this;
    }
    public function pageList($arr_list='[10,20,50,100,200]') {
    	$this->_table['page_list'] = $arr_list;
    	return $this;
    }
    public function onlyInfoPagination($flag=true) {
    	$this->_table['only_info_pagination'] = $flag;
    	return $this;
    }
    public function sidePagination($side='client') { // -未测试
    	$this->_table['side_pagination'] = $side;
    	return $this;
    }
    public function paginationHAlign($align='left') {
    	$this->_table['side_pagination'] = $side;
    	return $this;
    }
    public function paginationVAlign($align='top') { // bottom top both
    	$this->_table['side_pagination'] = $side;
    	return $this;
    }
    public function paginationDetailHAlign($align='left') {
		$this->_table['pagination_detail_h_align'] = $side;
    	return $this;
    }
    public function paginationPreText($text='&lsaquo;') {
		$this->_table['pagination_pre_text'] = $text;
    	return $this;
    }
    public function paginationNextText($text='&rsaquo;') {
		$this->_table['pagination_next_text'] = $text;
    	return $this;
    }
    public function striped($flag=false) {
    	$this->_table['striped'] = $flag;
    	return $this;
    }
    public function search($flag=false) {
    	$this->_table['search'] = $flag;
    	return $this;
    }
    public function searchAlign($align='left') {
    	$this->_table['search_align'] = $align;
    	return $this;
    }
    public function searchOnEnterKey($flag=false) { // --有问题
    	$this->_table['search_on_enter_key'] = $flag;
    	return $this;
    }
    public function strictSearch($flag=true) { // --
    	$this->_table['search_on_enter_key'] = $flag;
    	return $this;
    }
    public function searchText($text=true) { // --
    	$this->_table['search_text'] = $text;
    	return $this;
    }
    public function cardView($flag=true) { // --
    	$this->_table['card_view'] = $flag;
    	return $this;
    }
    public function detailView($flag=true) { // --
    	$this->_table['detail_view'] = $flag;
    	return $this;
    }
    


    public function selectItemName() {
    	$this->_table['select_item_name'] = $flag;
    	return $this;
    }

    // -- 状态修改链接
    public function setStatusUrl($url) {
    	if ( !$url ) $url="setStatus";
        $this->_statusUrl = $url;
        dump($this->_statusUrl );
        return $this;
    }
	// -- 彻底删除链接
    public function setDeleteTrueUrl($url) {
    	if ( $url ) $url = "delete";
        $this->_deleteTrueUrl = $url;
        return $this;
    }

	// -- 按钮
    public function button($title, $attr) {
        $this->_buttonList[] = array('title' => $title, 'attr' => $attr, 'href' => $attr['href']);
        return $this;
    }
	// -- 新增按钮
    public function buttonNew($href, $title='新增', $attr=array()) {
        $attr['href'] = $href;
        $attr['class'] = 'btn btn-ajax btn-success';
        return $this->button($title, $attr);
    }
	// -- ajax按钮 attr中设置（'hide-data' => 'true'）表示，不必须要勾选对象，即不必须ids有值
    public function ajaxButton($url, $params, $title, $attr=array()) {
        $attr['class'] = 'btn ajax-post btn-default';
        $attr['url'] = $this->addUrlParam($url, $params);
        $attr['target-form'] = 'ids';
        return $this->button($title, $attr);
    }
	// -- 模态弹窗按钮
    public function buttonModalPopup($url, $params, $title, $attr=array()) {
        // $attr data-title 模态框标题 target-form 要传输的数据
        $attr['modal-url'] = $this->addUrlParam($url, $params);
        $attr['data-role'] = 'modal_popup';
        return $this->button($title, $attr);
    }

    // -- 状态设置按钮
    public function buttonSetStatus($url, $status, $title, $attr) {
        $attr['class'] = isset($attr['class']) ? $attr['class'] : 'btn ajax-post btn-default';
        $attr['url'] = $this->addUrlParam($url, array('status' => $status));
        $attr['target-form'] = 'ids';
        return $this->button($title, $attr);
    }
    // -- 禁用按钮 0
    public function buttonDisable($url=null, $title='禁用', $attr=array()) {
        if ( !$url ) $url = $this->_statusUrl;
        $attr['class'] = 'btn ajax-post btn-warning';
        return $this->buttonSetStatus($url, 0, $title, $attr);
    }
	// -- 启用按钮 1
    public function buttonEnable($url=null, $title='启用', $attr=array()) {
        if ( !$url ) $url = $this->_statusUrl;
        $attr['class'] = 'btn ajax-post btn-info';
        return $this->buttonSetStatus($url, 1, $title, $attr);
    }
    // -- 软删除按钮 -1
    public function buttonDelete($url=null, $title='删除', $attr=array()) {
        if ( !$url ) $url = $this->_statusUrl;
        $attr['class'] = 'btn ajax-post btn-danger';
        return $this->buttonSetStatus($url, -1, $title, $attr);
    }
	// -- 还原按钮 1
    public function buttonRestore($url=null, $title='还原', $attr=array()) {
        if ( !$url ) $url = $this->_statusUrl;
        return $this->buttonSetStatus($url, 1, $title, $attr);
    }

	// 清空回收站按钮 - 删除软删除全部
    public function buttonClear($model=null) {
        return $this->button(lang('_CLEAR_OUT_'), array(
        	'class' => 'btn ajax-post btn-default tox-confirm',
        	'data-confirm' => lang('_CONFIRM_CLEAR_OUT_'),
        	'url' => url('', array('model' => $model)),
	        'target-form' => 'ids',
	        'hide-data' => 'true'
        ));
    }
	// 彻底删除按钮
    public function buttonDeleteTrue($url=null) {
        if ( !$url ) $url = $this->_deleteTrueUrl;
        $attr['class'] = 'btn ajax-post btn-default tox-confirm';
        $attr['data-confirm'] = lang('_CONFIRM_DELETE_COMPLETELY_');
        $attr['url'] = $url;
        $attr['target-form'] = 'ids';
        return $this->button(lang('_DELETE_COMPLETELY_'), $attr);
    }
    
    // 排序按钮
    public function buttonSort($href, $title='排序', $attr=array()) {
        $attr['href'] = $href;
        return $this->button($title, $attr);
    }
	// 筛选
    public function select($title='筛选', $name='key', $type='select', $des='', $attr, $arrdb='', $arrvalue=null) {
        if ( empty($arrdb) ) $this->_select[] = array('title' => $title, 'name' => $name, 'type' => $type, 'des' => $des, 'attr' => $attr, 'arrvalue' => $arrvalue);
        return $this;
    }
    // 
    public function selectPlateForm($id, $method, $action) {
       $this->_form[] = array('id'=>$id, 'method'=>$method, 'action'=>$action);
        return $this;
    }

    // -- 字段
    public function key($name, $title, $type, $opt=null, $width='150px') {
        $this->_keyList[] = array('name' => $name, 'title' => $title, 'type' => $type, 'opt' => $opt, 'width' => $width);
        return $this;
    }

	// -- 纯文本
    public function keyText($name, $title, $opt=null) {
        return $this->key($name, text($title), 'text', $opt);
    }
    // -- 字段长度截取
    public function keyTruncText($name, $title, $length) {
        return $this->key($name, $title, 'trunktext', $length);
    }
	// -- 主键
    public function keyId($name='id', $title='ID') {
        return $this->keyText($name, $title);
    }
    // -- 标题
    public function keyTitle($name='title', $title='标题') {
        return $this->keyText($name, $title);
    }

	// -- html
    public function keyHtml($name, $title, $width='150px') {
        return $this->key($name, op_h($title), 'html', null, $width);
    }
    // -- 链接
    public function keyLink($name, $title, $getUrl, $target="_self") {
        if ( is_string($getUrl) ) $getUrl = $this->createDefaultGetUrlFunction($getUrl);
        // if ( empty($name) ) $name = $title;
        return $this->key($name, $title, 'link', array('url' => $getUrl, 'target' => $target));
    }
	// -- 带替换表示的链接
    public function keyLinkByFlag($name, $title, $getUrl, $flag='id') {
        if ( is_string($getUrl) ) $getUrl = $this->ParseUrl($getUrl, $flag);
        return $this->key($name, $title, 'link', $getUrl);
    }
	// -- 图标
    public function keyIcon($name='icon', $title='图标') {
        return $this->key($name, $title, 'icon');
    }
    // -- 图片
    public function keyImage($name, $title) {
        return $this->key($name, $title, 'image');
    }

	// -- 映射
    public function keyMap($name, $title, $map) {
        return $this->key($name, $title, 'map', $map);
    }
    // -- 是否
    public function keyYesNo($name, $title) {
        $map = array(
        	0 => lang('no'),
        	1 => lang('yes')
        );
        return $this->keymap($name, $title, $map);
    }
    // -- 布尔
    public function keyBool($name, $title) {
        return $this->keyYesNo($name, $title);
    }
    // -- 状态
    public function keyStatus($name='status', $title='状态') {
        $map = array(
        	-1 => '删除',
        	0 => '禁用',
        	1 => '启用',
        	2 => '未审核'
        );
        return $this->key($name, $title, 'status', $map);
    }

    // -- 时间
    public function keyTime($name, $title) {
        return $this->key($name, $title, 'time');
    }
    // -- 创建时间
    public function keyCreateTime($name='create_time', $title='创建时间') {
        return $this->keyTime($name, $title);
    }
    // -- 更新时间
    public function keyUpdateTime($name='update_time', $title='更新时间') {
        return $this->keyTime($name, $title);
    }

    // - 用户标识
    public function keyUid($name='uid', $title='用户') {
        return $this->key($name, $title, 'uid');
    }
    // - 用户昵称
    public function keyNickname($name='uid', $title, $subtitle=null) {
        return $this->key($name, $title, $subtitle, 'nickname');
    }

    // -- 关联表字段显示+URL连接
    public function keyJoin($name, $title, $mate, $return, $model, $url='') {

        $map = array('name' => $name,'mate' => $mate, 'return' => $return, 'model' => $model, 'url' => $url);
        return $this->key($name, $title, 'Join', $map);
    }

	// 模态弹窗
    public function keyDoActionModalPopup($getUrl, $text, $title, $attr=array()) {
        $attr['data-role'] = 'modal_popup';
        if ( is_string($getUrl) ) $getUrl = $this->createDefaultGetUrlFunction($getUrl);

        $doActionKey = null;
        foreach ( $this->_keyList as $key ) {
            if ( $key['name'] === 'DOACTIONS' ) {
                $doActionKey = $key;
                break;
            }
        }
        if ( !$doActionKey) $this->key('DOACTIONS', $title, 'doaction', $attr);

        $doActionKey = null;
        foreach ( $this->_keyList as &$key ) {
            if ( $key['name'] == 'DOACTIONS' ) {
                $doActionKey = &$key;
                break;
            }
        }

        $doActionKey['opt']['actions'][] = array('text' => $text, 'get_url' => $getUrl, 'opt' => $attr);
        return $this;
    }

    public function action($getUrl, $text, $title='操作') {
        if ( is_string($getUrl) ) $getUrl = $this->createDefaultGetUrlFunction($getUrl);

        $doActionKey = null;
        foreach ( $this->_keyList as $key ) {
            if ( $key['name'] === 'DOACTIONS' ) {
                $doActionKey = $key;
                break;
            }
        }
        if ( !$doActionKey ) $this->key('DOACTIONS', $title, 'doaction', array());

        $doActionKey = null;
        foreach ( $this->_keyList as &$key ) {
            if ( $key['name'] == 'DOACTIONS' ) {
                $doActionKey = &$key;
                break;
            }
        }

        $doActionKey['opt']['actions'][] = array('text' => $text, 'get_url' => $getUrl);
        return $this;
    }
	// 编辑
    public function actionEdit($url='', $text='编辑') {
    	if ( $url=='' ) {
    		$action = request()->action();
    		$url = $action."Edit?id=###";
    	}
        return $this->action($url, $text);
    }
	public function actionStatus($flag='disable') { // -- 设置状态
        $status = [
            'enable' => ['number' => '1', 'name' => '启用'], // 启用
            'disable' => ['number' => '0', 'name' => '禁用'], // 禁用
            'delete' => ['number' => '-1', 'name' => '删除'], // 删除
            'restore' => ['number' => '1', 'name' => '还原'], // 还原
        ];
        $that = $this;

        $flags = explode(",", $flag);
        foreach ( $flags as $flag ) {
            $getUrl = function ($item) use ($that, $status, $flag) {
                $pattern = str_replace('###', $item['id'], $that->_statusUrl."?id=###"); // id替换
                return $that->addUrlParam($pattern, ['status' => $status[$flag]['number'],'table'=>$that->_db]);
            };
            $this->action($getUrl, $status[$flag]['name'], array('class' => 'ajax-get'));
        }

        return $this;
    }
    public function actionDelete($title="真删除") { // -- 硬删除
        $that = $this;

        $getUrl = function ($item) use ($that) {
            $pattern = str_replace('###', $item['id'], $that->_deleteTrueUrl."?id=###"); // id替换
            return  $that->addUrlParam($pattern, array('table'=>$that->_db));
        };

        return $this->action($getUrl, $title, array('class' => 'ajax-get'));
    }
    // --- 过滤
    public function map($name, $title, $type='text', $options=null, $extend=null) {
        switch ( $type ) {
        	case 'select':
        		return $this->mapSelect($name, $title, $options, $extend);
        	break;
        	case 'select2':
        		return $this->mapSelect2($name, $title, $options, $extend);
        	break;
        	default:
        		return $this->mapText($name, $title, $extend);
        }
    }
    public function mapText($name, $title, $extend=null) {
    	$this->_search[] = array(
        	'name' => $name,
        	'title' => $title,
            'type' => 'text',
            'extend' => $extend
        );

        return $this;
    }
    public function mapSelect($name, $title, $options) {
    	$this->_search[] = array(
        	'name' => $name,
        	'title' => $title,
            'type' => 'select',
            'options' => $options
        );

        return $this;
    }
    public function mapSelect2($name, $title, $options, $extend=null) {
		$this->_search[] = array(
        	'name' => $name,
        	'title' => $title,
            'type' => 'select2',
            'options' => $options,
            'extend' => $extend
        );

        return $this;
    }
    public function mapIn($name, $title, $options) {
    	return $this->mapSelect2($name, $title, $options, array('in' => true));
    }
    public function mapLike($name, $title, $extend=null) {
    	return $this->mapText($name, $title, array('like' => true));
    }
    public function mapStatus($name='status', $title='状态', $options=array(
        '-1'=>'已删除',
        '0'=>'禁用',
        '1'=>'启用',
        '2'=>'待审核'
    )) {
        return $this->mapIn($name, $title, $options);
    }
    public function mapTime($name="time", $title="时间") {
    	$this->_search[] = array(
        	'name' => $name,
        	'title' => $title,
            'type' => 'time',
        );
    }

    // -- 数据
    public function data($data) {
    	$this->_data = $data;
 
        $this->convertKey('map', 'text', function ($value, $key) { // 映射
            // return $key['opt'][$value];  //todo:: 这个地方会报错，未索引的数组 ,偶尔出现，应该与map冲突有关，keymap
        });
        // $this->convertKey('uid', 'text', function ($value) { // 用户标识
        //     $value = query_user(array('nickname', 'uid', 'space_url'), $value);
        //     return "<a href='" . $value['space_url'] . "' target='_blank'>[{$value[uid]}]" . $value['nickname'] . '</a>';
        // });
        // $this->convertKey('nickname', 'text', function ($value) { // 用户昵称
        //     $value = query_user(array('nickname', 'uid', 'space_url'), $value);
        //     return "<a href='" . $value['space_url'] . "' target='_blank'>[{$value[uid]}]" . $value['nickname'] . '</a>';
        // });
        $this->convertKey('time', 'text', function ($value) { // 时间
            if ( $value != 0 ) {
                return time_format($value);
            } else {
                return '-';
            }
        });
        $this->convertKey('trunktext', 'text', function ($value, $key) { // trunktext
            $length = $key['opt'];
            return msubstr($value, 0, $length);
        });

        $this->convertKey('text', 'html', function ($value) { // text
            return $value;
        });
        $this->convertKey('link', 'html', function ($value, $key, $item) { // link
            $value = htmlspecialchars($value);
            $getUrl = $key['opt']['url'];
            $url = $getUrl($item);

            if ( $key['title'] ) {
                return "<a href=\"$url\" target=\"" . $key['opt']['target'] . "\" class=\"J_menuItem\">" . $key['title'] . "</a>";
            } else {
                return "<a href=\"$url\" target=\"" . $key['opt']['target'] . "\" class=\"J_menuItem\">$value</a>";
            }
        });

        $this->convertKey('icon', 'html', function ($value, $key, $item) { // icon
            $value = htmlspecialchars($value);
            if ( $value == '' ) {
                $html = lang('none');
            } else {
                $html = "<i class=\"$value\"></i> $value";
            }
            return $html;
        });
        $this->convertKey('image', 'html', function ($value, $key, $item) { // image
            $value = htmlspecialchars($value);
            if ( intval($value) ) { // value是图片标识 - todo behero
                // $sc_src = get_cover($value, 'path');

                // $src = getThumbImageById($value, 80, 80);
                // $sc_src = $sc_src=='' ? $src : $sc_src;
            } else { // value是图片路径
                $sc_src = $value;
            }
            $html = "<div class='popup-gallery'><a title=\"" . lang('_VIEW_BIGGER_') . "\" href=\"$sc_src\"><img src=\"$sc_src\"/ style=\"height: 40px\"></a></div>";
            return $html;
        });
        $this->convertKey('doaction', 'html', function ($value, $key, $item) { // doaction
            $actions = $key['opt']['actions'];
            $result = array();
            foreach ( $actions as $action) {
                $getUrl = $action['get_url'];
                $linkText = $action['text'];
                $url = $getUrl($item);
                if ( isset($action['opt']) ) {
                    $content = array();
                    foreach ( $action['opt'] as $key => $value ) {
                        $value = htmlspecialchars($value);
                        $content[] = "$key=\"$value\"";
                    }
                    $content = implode(' ', $content);
                    if ( isset($action['opt']['data-role']) && $action['opt']['data-role'] == "modal_popup") {//模态弹窗
                        $result[] = "<a href=\" javascript: void(0);\" modal-url=\"$url\" " . $content . ">$linkText</a>";
                    } else {
                        $result[] = "<a href=\"$url\" " . $content . ">$linkText</a>";
                    }
                } else {
                    $result[] = "<a href=\"$url\">$linkText</a>";
                }
            }
            return implode(' ', $result);
        });
        $this->convertKey('Join', 'html', function ($value, $key) { // Join
          
            if ( $value != '' ) {
               
                $val = get_table_field($value, $key['opt']['mate'], $key['opt']['return'], $key['opt']['model']);

                if ( !$key['opt']['url'] ) {
                    return $val;
                } else {
                    $urld = url($key['opt']['url'], array($key['opt']['name'] => $value));
                    return "<a href=\"$urld\" target=\"_blank\">$val</a>";
                }
            } else {
                return '-';
            }
        });

        $statusUrl = $this->_statusUrl;
        $that = &$this;
        $this->convertKey('status', 'html', function ($value, $key, $item) use ($statusUrl, $that) { // 状态
            $map = $key['opt'];
            $text = $map[$value];
            if ( !$statusUrl ) return $text;

            $switchStatus = $value == 1 ? 0 : 1;
            $url = $that->addUrlParam($statusUrl, array('status' => $switchStatus, 'ids' => $item['id']));
            return "<a href=\"{$url}\" class=\"ajax-get\">$text</a>";
        });

        $this->convertKey('html', 'html', function ($value) { // 如果html为空
            if ( $value === '' ) return '<span style="color: #bbb;">' . lang('empty_braced') . '</span>';
            if ( $value === NULL ) return '<span style="color: #bbb;">' . lang('null_braced') . '</span>';
            return $value;
        });

        return $this->_data;
    }
    // -- 展示
    public function fetch($template=false, $vars=array(), $replace=array(), $config=array()) {
        foreach ( $this->_buttonList as &$button ) { // 编译buttonList中的属性
            $button['tag'] = isset($button['attr']['href']) ? 'a' : 'button';
            $this->addDefaultCssClass($button);
            $button['attr'] = $this->compileHtmlAttr($button['attr']);
        }

        $this->assign('buttonList', $this->_buttonList); // 按钮
        $this->assign('keyList', $this->_keyList); // 字段

        $this->assign('table', $this->_table); // 表格信息
        $this->assign('searches', $this->_search); // 字段信息

        return parent::fetch("page");
    }

    public function doSetStatus($model, $ids, $status = 1) {
        $id = array_unique((array)$ids);
        $rs = M($model)->where(array('id' => array('in', $id)))->save(array('status' => $status));
        if ( $rs === false ) {
            $this->error(lang('_ERROR_SETTING_') . lang('_PERIOD_'));
        } else {
        	$this->success(lang('_SUCCESS_SETTING_'), $_SERVER['HTTP_REFERER']);
        }
    }
    // -- 类型转换
    private function convertKey($from, $to, $convertFunction) {
        foreach ( $this->_keyList as &$key ) {
            if ( $key['type'] == $from ) {
                $key['type'] = $to;

                foreach ( $this->_data as &$data ) {
                    $value = &$data[$key['name']];
                    $value = $convertFunction($value, $key, $data);
                    unset($value);
                }
                unset($data);
            }
        }
        unset($key);
    }
    // -- 添加默认样式 - 按钮
    private function addDefaultCssClass(&$button) {
        if ( !isset($button['attr']['class']) ) {
            $button['attr']['class'] = 'btn btn-default';
        } else { // isset($button['attr']['class']
            $button['attr']['class'] .= ' btn btn-default';
        }
    }
    // -- 生成链接 id=### other_id={$other_id}
    private function createDefaultGetUrlFunction($pattern) {
        $explode = explode('|', $pattern);
        $pattern = $explode[0];
        $fun = empty($explode[1]) ? 'url' : $explode[1];

        return function ($item) use ($pattern, $fun) {
            $pattern = str_replace('###', $item['id'], $pattern); // id替换

            $view = new \think\View(); // 其他变量替换
            $view->assign($item);

            $pattern = $view->display($pattern);

            return $fun($pattern);
        };
    }
    // -- 附加查询参数
    public function addUrlParam($url, $params) {
        if ( strpos($url, '?') === false ) {
            $seperator = '?';
        } else {
            $seperator = '&';
        }

        $params = http_build_query($params);
        return $url . $seperator . $params;
    }
	// 自动处理清空回收站
    public function clearTrash($model='') {
        if ( IS_POST ) {
            if ( $model != '' ) {
                $aIds = I('post.ids', array());
                if ( !empty($aIds) ) {
                    $map['id'] = array('in', $aIds);
                } else {
                    $map['status'] = -1;
                }

                $result = model($model)->where($map)->delete();
                if ( $result ) $this->success(lang('_SUCCESS_TRASH_CLEARED_', array('result' => $result)));
                $this->error(lang('_TRASH_ALREADY_EMPTY_'));
            } else {
                $this->error(lang('_TRASH_SELECT_'));
            }
        }
    }
    // 执行彻底删除数据，只适用于无关联的数据表
    public function doDeleteTrue($model, $ids) {
        $ids = is_array($ids) ? $ids : explode(',', $ids);
        model($model)->where(array('id' => array('in', $ids)))->delete();
        $this->success(lang('_SUCCESS_DELETE_COMPLETELY_'), $_SERVER['HTTP_REFERER']);
    }
	// -- 解析Url - 链接 标识
    private function ParseUrl($pattern, $flag) {
        return function ($item) use ($pattern, $flag) { // 返回闭包函数
            $pattern = str_replace('###', $item[$flag], $pattern); // id替换

            $view = new \think\View(); // 其他变量替换
            $view->assign($item);
            $pattern = $view->display($pattern);

            return url($pattern);
        };
    }
}